package com.ecarx.vhaltests.hy11.testcases.system.seatadjustment;

import static android.car.VehiclePropertyIds.SEAT_FORE_AFT_POS;

import android.car.VehicleAreaSeat;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

// HY11 [SW_FC_004_043]第一排座椅滑移1stRowSeatLengthAdjustment 第一排乘客座椅前后滑移百分比
public class SeatForeAftPosTest extends VhalUnitTestBase {
    public static final String TAG = "SEAT_FORE_AFT_POS";
    private static int halValueRow1Left;
    private static int halStatusRow1Left;
    private static int halValueRow1Right;
    private static int halStatusRow1Right;
    final static Object lockRow1Left = new Object();
    final static Object lockRow1Right = new Object();


    private static final IpcpClient ipcpClient = new IpcpClient();

    @BeforeClass
    public static void testInit() {
        mCarPropertyMgr.registerCallback(carPropertyEventCallback, SEAT_FORE_AFT_POS, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        setConfig();
    }

    @Test
    public void testSeatForeAftPosNotAvail() {
        setSeatForeAftPosNotAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 1);
        Assert.assertEquals(halStatusRow1Right, 1);
        testSeatForeAftPosNotAvailLeft();
        testSeatForeAftPosNotAvailRight();
    }

    @Test
    public void testSeatForeAftPosAvail() {
        setSeatForeAftPosAvail();
        lockRow1Left();
        lockRow1Right();
        Assert.assertEquals(halStatusRow1Left, 0);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatForeAftPosIsAvailLeft();
        testSeatForeAftPosIsAvailRight();
    }


    @Test
    public void testSeatForeAftPosRow1Left50() {
        setSeatForeAftPosAvail();
        setCDMDriver(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);
    }

    @Test
    public void testSeatForeAftPosRow1Left50QFNotGood() {//信号质量差的时候收到的百分比忽略
        setSeatForeAftPosAvail();
        setCDMDriver(50, 3);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        Assert.assertEquals(halStatusRow1Left, 0);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);

        setCDMDriver(60, 2);
        lockRow1Left();
        Assert.assertEquals(halValueRow1Left, 50);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_LEFT, 50);
    }

    @Test
    public void testSeatForeAftPosRow1Right50() {
        setSeatForeAftPosAvail();
        setCDMPass(50, 3);
        lockRow1Right();
        Assert.assertEquals(halValueRow1Right, 50);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);
    }

    @Test
    public void testSeatForeAftPosRow1Right50QFNotGood() {//信号质量差的时候收到的百分比忽略
        setSeatForeAftPosAvail();
        setCDMPass(50, 3);
        lockRow1Right();
        Assert.assertEquals(halValueRow1Right, 50);
        Assert.assertEquals(halStatusRow1Right, 0);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);

        setCDMPass(50, 2);
        lockRow1Right();
        Assert.assertEquals(halValueRow1Right, 50);
        testSeatForeAftPosValue(VehicleAreaSeat.SEAT_ROW_1_RIGHT, 50);
    }

    public void testSeatForeAftPosValue(int areaId, int value) {
        int halValue = mCarPropertyMgr.getIntProperty(SEAT_FORE_AFT_POS, areaId);
        Assert.assertEquals(halValue, value);
    }

    public void testSeatForeAftPosIsAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_FORE_AFT_POS, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertTrue(isAvail);
    }

    public void testSeatForeAftPosIsAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_FORE_AFT_POS, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertTrue(isAvail);
    }

    public void testSeatForeAftPosNotAvailLeft() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_FORE_AFT_POS, VehicleAreaSeat.SEAT_ROW_1_LEFT);
        Assert.assertFalse(isAvail);
    }

    public void testSeatForeAftPosNotAvailRight() {
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(SEAT_FORE_AFT_POS, VehicleAreaSeat.SEAT_ROW_1_RIGHT);
        Assert.assertFalse(isAvail);
    }

    public void lockRow1Left() {
        try {
            synchronized (lockRow1Left) {
                lockRow1Left.wait(1000);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void lockRow1Right() {
        try {
            synchronized (lockRow1Right) {
                lockRow1Right.wait(1000);
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static CarPropertyManager.CarPropertyEventCallback carPropertyEventCallback = new CarPropertyManager.CarPropertyEventCallback() {
        @Override
        public void onChangeEvent(CarPropertyValue carPropertyValue) {
            try {
                Log.d(TAG, carPropertyValue.toString());

                int areaId = carPropertyValue.getAreaId();
                if (areaId == VehicleAreaSeat.SEAT_ROW_1_LEFT) {
                    halValueRow1Left = (int) carPropertyValue.getValue();
                    halStatusRow1Left = carPropertyValue.getStatus();
                    synchronized (lockRow1Left) {
                        lockRow1Left.notify();
                    }
                } else if (areaId == VehicleAreaSeat.SEAT_ROW_1_RIGHT) {
                    halValueRow1Right = (int) carPropertyValue.getValue();
                    halStatusRow1Right = carPropertyValue.getStatus();
                    synchronized (lockRow1Right) {
                        lockRow1Right.notify();
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }

        @Override
        public void onErrorEvent(int i, int i1) {

        }
    };

    private static void setConfig() {
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        int ccSvcId = 135;
        int[] ccOperIds = {200};
        int cc1000SvcId = 136;
        int[] cc1000OperIds = {200};
        int cdmSvcId = 36;
        int[] cdmOperIds = {999};

        boolean retCem = ipcpClient.setConfig(cemSvcId, cemOperIds);
        Assert.assertTrue(retCem);

        boolean retCc = ipcpClient.setConfig(ccSvcId, ccOperIds);
        Assert.assertTrue(retCc);

        boolean retCdm = ipcpClient.setConfig(cdmSvcId, cdmOperIds);
        Assert.assertTrue(retCdm);

        boolean retCc1000 = ipcpClient.setConfig(cc1000SvcId, cc1000OperIds);
        Assert.assertTrue(retCc1000);

    }

    public void setSeatForeAftPosNotAvail() {
        setCarConfig(6, 5);
        setCarConfig1000(3, 6);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_ABANDON);
    }


    public void setSeatForeAftPosAvail() {
        setCarConfig(4, 2);
        setCarConfig1000(2, 2);
        setCDM(0, 2, 0, 2);
        setCEM(VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_DRIVING);
    }

    public void setCEM(int carMode, int usgMode) {
        int cemSvcId = 37;
        int cemOperId = 999;
        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {carMode, usgMode};
        boolean cemResult = ipcpClient.setCEMSignal(cemSvcId, cemOperId, cems, cemValues);
        Assert.assertTrue(cemResult);
        delay();
    }

    public void setCarConfig(int cc184, int cc472) {
        int ccSvcId = 135;
        int ccOperId = 200;
        int[] configs = {184, 472};
        int[] values = {cc184, cc472};
        boolean ccResult = ipcpClient.setCarconfigSignal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    private void setCarConfig1000(int cc1173, int cc1174) {
        int ccSvcId = 136;
        int ccOperId = 200;
        int[] configs = {1173, 1174};
        int[] values = {cc1173, cc1174};
        boolean ccResult = ipcpClient.setCarconfig1000Signal(ccSvcId, ccOperId, configs, values);
        Assert.assertTrue(ccResult);
        delay();
    }

    public void setCDMDriver(int drvrSeatPosPercSeatPosSldPerc, int drvrSeatPosPercSeatPosSldQF) {
        setCDM(drvrSeatPosPercSeatPosSldPerc, drvrSeatPosPercSeatPosSldQF, 0, 0);
    }

    public void setCDMPass(int passSeatPosPercSeatPosSldPerc, int passSeatPosPercSeatPosSldQF) {
        setCDM(0, 0, passSeatPosPercSeatPosSldPerc, passSeatPosPercSeatPosSldQF);
    }

    public void setCDM(int drvrSeatPosPercSeatPosSldPerc, int drvrSeatPosPercSeatPosSldQF, int passSeatPosPercSeatPosSldPerc, int passSeatPosPercSeatPosSldQF) {
        int cdmSvcId = 36;
        int cdmOperId = 999;
        String[] cdms = {"drvrSeatPosPercSeatPosSldPerc", "drvrSeatPosPercSeatPosSldQF", "passSeatPosPercSeatPosSldPerc", "passSeatPosPercSeatPosSldQF"};
        int[] cdmValues = {drvrSeatPosPercSeatPosSldPerc * 10, drvrSeatPosPercSeatPosSldQF, passSeatPosPercSeatPosSldPerc * 10, passSeatPosPercSeatPosSldQF};
        boolean cdmResult = ipcpClient.setCDMSignal(cdmSvcId, cdmOperId, cdms, cdmValues);
        Assert.assertTrue(cdmResult);
        delay();
    }

}